#include "../header/JoueurOrdinateur.h"

int JoueurOrdinateur::jouer() {

	string couleur;
	int pionChoisi;
	Pion *pion;
	int caseLigneChoisi = 0;
	int caseColonneChoisi = 0;
	if (this->numero == 1) {
		couleur = "Vert";
	} else {
		couleur = "Rouge";
	}
	cout << "Tour du joueur numero " << this->numero << " ( " << couleur
			<< " ) " << endl;
	/* pionChoisi = Random */

	pionChoisi = this->random(0, this->nbPion);
	/* Choix de la case */
	/* Position d'impala johns */
	/* Si impala est en haut ou en bas */
	if (this->map->getImpala()->getPosX() == 0
			|| this->map->getImpala()->getPosX() == 6) {
		/* Random case */
		int r = this->random(1, 6);
		while (!this->map->getPlateau(r, this->map->getImpala()->getPosY())->isCaseVide()) {
			r = this->random(1, 6);
		}
		caseLigneChoisi = r;
		caseColonneChoisi = this->map->getImpala()->getPosY();

		/* Si impala est a gauche ou a droite */
	} else {
		/* Random case */
		int r = this->random(1, 7);
		while (!this->map->getPlateau(this->map->getImpala()->getPosX(), r)->isCaseVide()) {
			r = this->random(1, 7);
		}
		caseLigneChoisi = this->map->getImpala()->getPosX();
		caseColonneChoisi = r;
	}

	/*Ajout Pion Map */

	pion = this->main[pionChoisi];
	cout << "Pion choisi par l'ordinateur : " << pion->getName() << endl;
	cout << "Case choisi par l'ordinateur : Ligne: " << caseLigneChoisi
			<< " Colonne: " << caseColonneChoisi << endl;

	this->map->ajoutPion(pion, caseLigneChoisi, caseColonneChoisi);

	/*Suppression du pion */
	supprimerPionMain(pion->getName());

	/*	on fait les differents check des animaux */

	if (pion->getName().compare("Zebre") == 0) {
		this->map->checkZebre(caseLigneChoisi, caseColonneChoisi);
	}
	if (pion->getName().compare("Lion") == 0) {
		this->map->checkLion(caseLigneChoisi, caseColonneChoisi);

	}

	if (pion->getName().compare("Gazelle") == 0) {
		this->map->checkGazelle(caseLigneChoisi, caseColonneChoisi, true);
	}

	/* Faire bouger Impala*/
	int dplImp = this->random(1, 3);
	this->map->getImpala()->deplacementImpala(dplImp);
	bool ligneOk = false;

	if (this->map->nbCaseRestant() != 0) {
		while (!ligneOk) {
			if (this->map->getImpala()->getPosX() == 0
					|| this->map->getImpala()->getPosX() == 6) {
				for (int i = 1; i < 6; i++) {
					if (this->map->getPlateau(i,
							this->map->getImpala()->getPosY())->isCaseVide()) {
						ligneOk = true;
					}
				}
				if (!ligneOk) {
					this->map->getImpala()->deplacementImpala(1);
				}
			} else if (this->map->getImpala()->getPosY() == 0
					|| this->map->getImpala()->getPosY() == 7) {
				for (int i = 1; i < 7; i++) {
					if (this->map->getPlateau(this->map->getImpala()->getPosX(),
							i)->isCaseVide()) {
						ligneOk = true;
					}
				}
				if (!ligneOk) {
					this->map->getImpala()->deplacementImpala(1);
				}
			}
		}
		cout << "L'ordinateur a deplacer Impala de " << dplImp << " case(s) "
				<< endl;
	}
	return 0;
}
